package com.atguigu.yygh.user.service.impl;

import com.atguigu.yygh.client.DictFeignClient;
import com.atguigu.yygh.common.handler.JwtHelper;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.mapper.PatientMapper;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 就诊人表 服务实现类
 * </p>
 *
 * @author xin Zhou
 * @since 2024-02-02
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private DictFeignClient dictFeignClient;


//    查询就诊人信息
    @Override
    public List<Patient> findAll(String token) {
        Long userId = JwtHelper.getUserId(token);
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Patient> patients = baseMapper.selectList(queryWrapper);
//        封装证件类型 --->{把编号改成字典数据}
        patients.stream().forEach(this::packagePatient);
        return patients;
    }

//    回显就诊人信息
    @Override
    public Patient detail(Long id) {
//        获取就诊人信息
        Patient patient = baseMapper.selectById(id);
        this.packagePatient(patient);
        return patient;
    }

    private void packagePatient(Patient item) {
//        在字典里查询数据,code --> 文字
        String result = dictFeignClient.getNameByValue(Long.parseLong(item.getCertificatesType()));
        item.getParam().put("certificatesTypeString",result);
        item.getParam().put("provinceString",dictFeignClient.getNameByValue(Long.parseLong(item.getProvinceCode())));
        item.getParam().put("cityString",dictFeignClient.getNameByValue(Long.parseLong(item.getCityCode())));
        item.getParam().put("districtString",dictFeignClient.getNameByValue(Long.parseLong(item.getDistrictCode())));
    }
}
